package com.ysd.modules.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ysd.common.exception.RRException;
import com.ysd.common.utils.R;
import com.ysd.modules.app.entity.ChapterEntity;
import com.ysd.modules.sys.entity.Chapter;
import com.ysd.modules.sys.entity.CourseEntity;
import com.ysd.modules.sys.entity.SysChapterVideo;
import com.ysd.modules.sys.model.StatusReqs;
import com.ysd.modules.sys.service.ChapterService;
import com.ysd.modules.sys.service.CourseService;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 章节
 * @author dapeng
 * @since 2021-07-13
 */
@RestController
@RequestMapping("/chapter")
@Api(description = "PC关于章节的所有接口")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private CourseService courseService;

    /**
     * 列表查询
     * @param req
     * @return R
     */
    @GetMapping("/list")
    @RequiresPermissions("chapter:list")
    public R listSearch(@RequestParam Map<String, Object> req){
        IPage<Chapter> page = chapterService.listSearch(req);
        R ok = R.ok();
        ok.put("data", page.getRecords());
        ok.put("count", page.getTotal());
        return ok;
    }

    /**
     * 查询详情
     * @param id
     * @return R
     */
    @GetMapping("/detail/search")
    @RequiresPermissions("chapter:detail:search")
    public R listSearch(@RequestParam Integer id){
        Chapter chapter = chapterService.detailSearch(id);
        return R.ok().put("data", chapter);
    }

    /**
     * 新增/修改
     * @param req
     * @return R
     */
    @PostMapping("/save")
    @RequiresPermissions("chapter:save")
    public R save(@RequestBody Chapter req){
        this.verifyForm(req);
        if (req.getId() == null || 0 == req.getId()) {
            chapterService.save(req);
        } else {
            chapterService.updateById(req);
        }
        return R.ok();
    }

    /**
     * 批量修改状态
     * @param req
     * @return R
     */
    @PostMapping("/status/modify")
    @RequiresPermissions("chapter:status:modify")
    public R statusModify(@RequestBody StatusReqs req){
        chapterService.statusModify(req.getReq());
        return R.ok();
    }

    /**
     * 校验数据
     * @param chapter
     * @return
     */
    public void verifyForm(Chapter chapter) {
        if(StringUtils.isBlank(chapter.getName())){
            throw new RRException("章节名称不能为空");
        }

        if(chapter.getCourseId() == null || 0 == chapter.getCourseId()){
            throw new RRException("课程不能为空");
        }

        //菜单
        if(chapter.getOrderNum() == null || chapter.getOrderNum() == 0){
            throw new RRException("章节编号不能为空");
        }

        CourseEntity course = courseService.getById(chapter.getCourseId());
        if (course == null) {
            throw new RRException("课程不存在");
        }
        chapter.setCourseName(course.getName());
    }


    @GetMapping("getVideoByChapter")
    @ApiOperation("视频管理-视频查看(条件分页)")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = Chapter.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "章节ID(非必传)", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "searchName", value = "视频名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "当前页数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", dataType = "int"),
    })
    public R getVideoByChapter(Integer cid, String searchName,Integer page,Integer limit){
        IPage<Chapter> chapterList = chapterService.getVideoByChapter(cid, searchName, page, limit);
        R ok = R.ok();
        ok.put("data", chapterList.getRecords());
        ok.put("count", chapterList.getTotal());
        return ok;
    }

    @PostMapping("addSysChapterVideo")
    @ApiOperation("视频管理-增加视频")
    public R addSysChapterVideo(@RequestBody SysChapterVideo sysChapterVideo){
        int len = chapterService.addSysChapterVideo(sysChapterVideo);
        R ok = R.ok();
        ok.put("data", len);
        return ok;
    }

    @PostMapping("delSysChapterVideo")
    @ApiOperation("视频管理-删除视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "视频ID", paramType = "query", dataType = "int"),
    })
    public R delSysChapterVideo(Integer id){
        int len = chapterService.delSysChapterVideo(id);
        R ok = R.ok();
        ok.put("data", len);
        return ok;
    }

    @PostMapping("updateSysChapterVideo")
    @ApiOperation("视频管理-修改视频(id必传)")
    public R updateSysChapterVideo(@RequestBody SysChapterVideo sysChapterVideo){
        int len = chapterService.updateSysChapterVideo(sysChapterVideo);
        R ok = R.ok();
        ok.put("data", len);
        return ok;
    }

    @GetMapping("/getChapterListByParams")
    @RequiresPermissions("chapter:list")
    public R getChapterListByParams(@RequestParam Map<String, Object> req){
        IPage<Chapter> page = chapterService.getChapterListByParams(req);
        R ok = R.ok();
        ok.put("data", page.getRecords());
        ok.put("count", page.getTotal());
        return ok;
    }
}

